package com.wbpay.sdk.request;

import android.content.Context;
import android.text.TextUtils;

import com.wbpay.sdk.constant.Constants;
import com.wbpay.sdk.formatter.Formatter;
import com.wbpay.sdk.formatter.UserJsonFormatter;
import com.wbpay.sdk.formatter.UserXmlFormatter;
import com.wbpay.sdk.framework.ResponseCallback;
import com.wbpay.sdk.http.HttpCallback;
import com.wbpay.sdk.http.IHttp;
import com.wbpay.sdk.http.Response;
import com.wbpay.sdk.http.impl.HttpImpl;
import com.wbpay.sdk.model.SdkInfo;
import com.wbpay.sdk.params.User;
import com.wbpay.sdk.utils.Log;
import com.wbpay.sdk.utils.ParamsValidator;
import com.wbpay.sdk.utils.Utils;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * Created by ck01- on 2017/7/4.
 */

public class UserRequestImpl implements IUserRequest {

    private Context mContext;
    private IHttp mHttp;

    public UserRequestImpl(Context context) {
        this.mContext = context;
        mHttp = new HttpImpl(context);
    }

    @Override
    public void login(String username, String password, final ResponseCallback<User> callback) {
        try {
            ParamsValidator.checkUsername(username);
            ParamsValidator.checkPwd(password);
        } catch (IllegalArgumentException e) {
            Response<User> response = new Response<User>();
            response.setCode(Constants.STATE_PARAM_ERR);
            response.setMsg(e.getMessage());
            callback.onCallback(response);
            return;
        } catch (Exception e) {
            Response<User> response = new Response<User>();
            response.setCode(Constants.STATE_SYS_ERR);
            response.setMsg(Constants.SYS_ERR_DESC);
            return;
        }

        Map<String, String> params = new HashMap<String, String>();
        params.put("id", Constants.SSO_LOGIN_ID);
        params.put("userName", username);
        params.put("password", Utils.string2MD5(password));
        params.put("userIp", Utils.getLocalIp());
        params.put("s", Utils.string2MD5(Constants.SSO_LOGIN_ID + username + Utils.getLocalIp()
                + Constants.SSO_LOGIN_KEY));

        mHttp.post(SSO_LOGIN_URL, params, null, new HttpCallback() {
            @Override
            public void onSuccess(String content) {
                // Log.d(Log.TAG, "content : " + content);
                Formatter formatter = new UserXmlFormatter();
                Response<User> response = (Response<User>) formatter.format(content);
                if (callback != null) {
                    callback.onCallback(response);
                }
            }

            @Override
            public void onFailure(int error, String msg) {
                // 所有的异常信息都显示系统异常
                Log.d(Log.TAG, "msg : " + msg);
                Response<User> response = new Response<User>();
                response.setCode(Constants.STATE_SYS_ERR);
                response.setMsg(Constants.SYS_ERR_DESC);
                if (callback != null) {
                    callback.onCallback(response);
                }
            }
        });
    }

    @Override
    public void registerByUserName(String username, String password, final ResponseCallback<User> callback) {
        String channel = SdkInfo.get(mContext).getChannel();
        String game = SdkInfo.get(mContext).getAppId();
        Map<String, String> params = new HashMap<String, String>();
        params.put("channel", channel);
        params.put("game", game);
        params.put("username", username);
        params.put("password", password);
        params.put("s", Utils.string2MD5(channel + game + username + password + KEY).toLowerCase(Locale.getDefault()));
        mHttp.post(USERNAME_REGISTER_URL, params, null, new HttpCallback() {
            @Override
            public void onSuccess(String content) {
                Log.d(Log.TAG, "content : " + content);
                Formatter formatter = new UserJsonFormatter();
                Response<User> response = (Response<User>) formatter.format(content);
                if (callback != null) {
                    callback.onCallback(response);
                }
            }

            @Override
            public void onFailure(int error, String msg) {
                Response<User> response = new Response<User>();
                response.setCode(Constants.STATE_SYS_ERR);
                response.setMsg(Constants.SYS_ERR_DESC);
                if (callback != null) {
                    callback.onCallback(response);
                }
            }
        });

    }

    @Override
    public void registerByTelephne(String telphone, String password, final ResponseCallback<User> callback) {
        String channel = SdkInfo.get(mContext).getChannel();
        String game = SdkInfo.get(mContext).getAppId();
        Map<String, String> params = new HashMap<String, String>();
        params.put("channel", channel);
        params.put("game", game);
        params.put("username", telphone);
        params.put("password", password);
        params.put("s", Utils.string2MD5(channel + game + telphone + password + KEY).toLowerCase(Locale.getDefault()));
        mHttp.post(CELLPHONE_REGISTER_URL, params, null, new HttpCallback() {
            @Override
            public void onSuccess(String content) {
                Log.d(Log.TAG, "content : " + content);
                Formatter formatter = new UserJsonFormatter();
                Response<User> response = (Response<User>) formatter.format(content);
                if (callback != null) {
                    callback.onCallback(response);
                }
            }

            @Override
            public void onFailure(int error, String msg) {
                Response<User> response = new Response<User>();
                response.setCode(Constants.STATE_SYS_ERR);
                response.setMsg(Constants.SYS_ERR_DESC);
                if (callback != null) {
                    callback.onCallback(response);
                }
            }
        });
    }

    @Override
    public void fetchCaptcha(String telphone, final ResponseCallback<Void> callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("username", telphone);
        mHttp.post(SMS_URL, params, null, new HttpCallback() {
            @Override
            public void onSuccess(String content) {
                Log.d(Log.TAG, "content : " + content);
                Formatter formatter = new Formatter<Void>();
                Response<Void> response = formatter.format(content);
                if (callback != null) {
                    callback.onCallback(response);
                }
            }

            @Override
            public void onFailure(int error, String msg) {
                Response<Void> response = new Response<Void>();
                response.setCode(Constants.STATE_SYS_ERR);
                response.setMsg(Constants.SYS_ERR_DESC);
                if (callback != null) {
                    callback.onCallback(response);
                }
            }
        });
    }

    @Override
    public void verifyCaptcha(String telphone, String captcha, final ResponseCallback<Void> callback) {
        String channel = SdkInfo.get(mContext).getChannel();
        String game = SdkInfo.get(mContext).getAppId();
        Map<String, String> params = new HashMap<String, String>();
        params.put("channel", channel);
        params.put("game", game);
        params.put("username", telphone);
        params.put("vcode", captcha);
        mHttp.post(SMS_CHECK_URL, params, null, new HttpCallback() {
            @Override
            public void onSuccess(String content) {
                Log.d(Log.TAG, "content : " + content);
                Formatter formatter = new Formatter<Void>();
                Response<Void> response = formatter.format(content);
                if (callback != null) {
                    callback.onCallback(response);
                }
            }

            @Override
            public void onFailure(int error, String msg) {
                Response<Void> response = new Response<Void>();
                response.setCode(Constants.STATE_SYS_ERR);
                response.setMsg(Constants.SYS_ERR_DESC);
                if (callback != null) {
                    callback.onCallback(response);
                }
            }
        });
    }

    @Override
    public void checkRegisterUser(String username, final ResponseCallback<Void> callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("type", "u");
        params.put("u", username);
        mHttp.post(REG_QUERY_URL, params, null, new HttpCallback() {
            @Override
            public void onSuccess(String content) {
                Log.d(Log.TAG, "content : " + content);
                Response<Void> response = new Response<Void>();
                if (Constants.STATE_REG_QUERY_SUC.equals(content)) {
                    response.setCode(Constants.STATE_SUC);
                } else {
                    if (TextUtils.isEmpty(content)) {
                        response.setCode(Constants.STATE_TIMEOUT);
                        response.setMsg(Constants.CONNECT_TIMEOUT_DESC);
                    } else {
                        response.setCode(Constants.STATE_REQ_QUERY_ERR);
                        response.setMsg(content);
                    }
                }
                if (callback != null) {
                    callback.onCallback(response);
                }
            }

            @Override
            public void onFailure(int error, String msg) {
                Response<Void> response = new Response<Void>();
                response.setCode(Constants.STATE_SYS_ERR);
                response.setMsg(Constants.CONNECT_TIMEOUT_DESC);
                if (callback != null) {
                    callback.onCallback(response);
                }
            }
        });
    }
}
